 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : WebQQCoreAsync
 * Package  : iqq.im.action
 * File     : GetCaptchaImageAction.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2012-9-6
 * License  : Apache License 2.0 
 */
package iqq.mk.action;

import iqq.im.QQActionListener;
import iqq.im.QQException;
import iqq.im.QQException.QQErrorCode;
import iqq.im.action.AbstractHttpAction;
import iqq.im.bean.QQAccount;
import iqq.im.core.QQContext;
import iqq.im.event.QQActionEvent;
import iqq.im.http.QQHttpRequest;
import iqq.im.http.QQHttpResponse;
import iqq.im.util.StringHelper;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.json.JSONException;

/**
 *
 * 获取验证码图片
 *
 * @author solosky
 */
public class CapUnionGetImageAction extends AbstractHttpAction {
	
	public CapUnionGetImageAction(QQContext context,
			QQActionListener listener) {
		super(context, listener);
	}
	/** {@inheritDoc} */
	@Override
	protected void onHttpStatusOK(QQHttpResponse response) throws QQException,
			JSONException {
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(response.getResponseData());
			notifyActionEvent(QQActionEvent.Type.EVT_OK, ImageIO.read(in));
		} catch (IOException e) {
			notifyActionEvent(QQActionEvent.Type.EVT_ERROR, new QQException(QQErrorCode.UNKNOWN_ERROR, e));
		}
	}
	/** 
		aid:1600000084
		captype:
		protocol:http
		clientype:2
		disturblevel:
		apptype:2
		noBorder:noborder
		showtype:embed
		uin:247262232
		cap_cd:7K83z3-nUVSUK4pSBmQ-7eZi3OMS3YiPvZnCqf9FqdqlUpganrlwQQ**
		rnd:667166
		rand:0.4204409882514957
		sig:gHt2mZIT_q592sAssgtybWHEE0f7E_C_5Zkzb_FD5zjdA2MSfnFXa6pnfcUZcFrgAxECVaeqnzIogHpiZWM4fjvp88Erpn7GXyCKdTXxlSOpLdHr65_xwbA** */
	@Override
	protected QQHttpRequest onBuildRequest() throws QQException, JSONException {
		QQHttpRequest req = createHttpRequest("GET", "http://captcha.qq.com/getimgbysig");
		QQAccount account = getContext().getAccount(); 
		String paramPath = "?aid=1600000084&captype=&protocol=http&clientype=2&disturblevel=&apptype=2&noBorder=noborder&showtype=embed&uin={0}&cap_cd={1}&rnd={2}";
		String subUrl = StringHelper.format(paramPath,account.getUin(),getContext().getSession().getCapCode(),getContext().getSession().getClientId()+"");
		req.addHeader("Referer", "http://captcha.qq.com/cap_union_show_new"+subUrl);
		req.addHeader("Host", "captcha.qq.com");
		req.addGetValue("aid", "1600000084");
		req.addGetValue("captype", "");
		req.addGetValue("protocol", "http");
		req.addGetValue("clientype", "2");
		req.addGetValue("disturblevel", "");
		req.addGetValue("apptype", "2");
		req.addGetValue("noBorder", "noborder");
		req.addGetValue("showtype", "embed");		
		req.addGetValue("uin", account.getUin() + "");
		req.addGetValue("cap_cd", getContext().getSession().getCapCode());	
		req.addGetValue("rnd", getContext().getSession().getClientId()+"");	
		req.addGetValue("sig", getContext().getSession().getNewSig());		
		req.addGetValue("rand", Math.random() + "");
		return req;
	}	
}
